package concurrent.demo2;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 闭锁示例
 *
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2019/4/1/001 下午 8:01
 */
public class Work implements Runnable {
  private CountDownLatch latch;
  private String name;

  public Work(CountDownLatch latch, String name) {
    this.latch = latch;
    this.name = name;
  }

  public void run() {
    this.doWork();
    try {
      TimeUnit.SECONDS.sleep(new Random().nextInt(10));
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(this.name + "活干完了");
    this.latch.countDown();
  }

  private void doWork() {
    System.out.println("正在干活");
  }
}
